home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 05 Programming / GRAPH.SRC < prev    next >
Text File  |  2019-04-13  |  9KB  |  343 lines

  1. /* graphic.c */
  2.  
  3. /*****************************************************************************/
  4. /*                    Basic IO routines for the VDC chip                     */
  5. /*                                                                           */
  6. /*  The following C128 routines were used to speed up operations to the VDC  */
  7. /*                                                                           */
  8. /*  BANK  ADDRESS   NAME                                                     */
  9. /* ````````````````````````````````````````````````````````````````````````` */
  10. /*  15    $CDDA   READREG       Read a value from VDC reg # in X, value is   */
  11. /*                              is returned in A.                            */
  12. /*                                                                           */
  13. /*  15    $CDCC   WRITEREG      Write a value to VDC reg # in A, value is in */
  14. /*                              X reg.                                       */
  15. /*                                                                           */
  16. /*  15    $CE0C   INIT80        Reset VDC to default conditions and load     */
  17. /*                              standard Commodore character set.            */
  18. /*                                                                           */
  19. /*  15    $CDD8   READ80        Reads a menory location from VDC memory, set */
  20. /*                              address to be read in VDC reg 18 & 19,       */
  21. /*                              value returned in A.                         */
  22. /*                                                                           */
  23. /*  15    $CDCA   WRITE80       Write a value to VDC's memory, set menory    */
  24. /*                              address in VDC reg 18 & 19, value in A       */
  25. /*                                                                           */
  26. /*****************************************************************************/
  27.  
  28.  
  29.  
  30. readvdc(portreg) /* read a vdc. reg. */ char portreg;
  31. {
  32. char portreg,a,y;
  33.     sys(0x0f,0xcdda,&a,&portreg,&y);
  34.     return(a);   
  35. }
  36.  
  37. writevdc(portreg,regbyte) char portreg,regbyte;
  38. {
  39. char y;
  40.  
  41. *   sys(0x0f,0xcdcc,®byte,&portreg,&y);
  42. }
  43.  
  44. pokevdc(addr,byte) char byte; unsigned addr;
  45. {
  46. char hi,lo,portreg,regbyte,y;
  47.  
  48.     hi = addr;
  49.     lo = addr >> 8;
  50.     portreg = 18;
  51.     regbyte = hi;
  52.     sys(0x0f,0xcdcc,®byte,&portreg,&y);
  53.     portreg = 19;
  54.     regbyte = lo;
  55.     sys(0x0f,0xcdcc,®byte,&portreg,&y);
  56.   ) sys(0x0f,0xcdca,&byte,&portreg,&y);
  57. }
  58.  
  59. peekvdc(addr) unsigned addr;
  60. {
  61. char hi,lo,regbyte,portreg,y;
  62.  
  63.     hi = addr;
  64.     lo = addr >> 8;
  65.     portreg = 18;
  66.     regbyte = hi;
  67.     sys(0x0f,0xcdcc,®byte,&portreg,&y);
  68.     portreg = 19;
  69.     regbyte = lo;
  70.     sys(0x0f,0xcdcc,®byte,&portreg,&y);
  71.     sys(0x0f,0xcdd8,®byte,&portreg,&y); 
  72.     return (regbyte);
  73. }
  74.  
  75. /*****************************************************************************/
  76. /*         Routines to enable and disable the hires mode of the VDC          */
  77. /*                                                                           */
  78. /*****************************************************************************/
  79.  
  80. hires() /* init hires mode */
  81. {
  82.     writevdc(25,135);
  83.     writevdc(26,113); 
  84.     writevdc(10,/);
  85. }
  86. hirescolor(foreground) char background;
  87. {
  88. /* the background is allways black */
  89.     writevdc(26,(foreground * 16));
  90. }
  91. clrhires(flag)
  92. int flag;
  93. {
  94. char byte;
  95. int i;
  96.  
  97.     writevdc(18,0);
  98.     writevdc(19,0);
  99.     if (flag) byte = 0;
  100.     else byte = 0xff;
  101.     for (i=0 ; i<=64 ; i++) {
  102.         writevdc(31,byte);
  103.         writevdc(30,0); 
  104.     }
  105. }
  106.  
  107. textmode(bank,charaddr) unsigned charaddr; int bank;
  108. {
  109.     writevdc(25,71);
  110.     clrhires(-1);
  111.     writevdc(10,96);
  112.     loadchar(bank,charaddr); /* c power's character set is at $3800 6/
  113. }
  114.  
  115. loadchar(flag,addr) int bank; unsigned addr;
  116. {
  117. char a,x,y;
  118. int ending,i,j;
  119.  
  120.     sys(0x0f,0xce0c,&a,&x,&y); /* use ROM routine INIT80 to load commodore char. set */
  121.     if (bank) {
  122.          ending = addr+2048;
  123.          writevdc(18,32);
  124.          writevdc(19,0);
  125.          for (i=addr ; i<=ending ; i=i+8) {
  126.              for (j=0 ; j<=7 ; j++) {
  127.                  writevdc(31,peek(15,i+j));
  128.              } 
  129.              for (j=8 ; j<=15 ; j++) {
  130.                  writevdc(31,0);
  131.              } 
  132.          }
  133.     } 
  134. }
  135.  
  136. /******************************************************************************/
  137. /*               Ploting and drawing routines for the VDC                     */
  138. /*                                                                            */
  139. /* These would be much faster if written in M.L., but are O.K. for doing      */
  140. /* drawings, but wouldn't do for games.                                       */
  141. /*                                                                            */
  142. /******************************************************************************/
  143.  
  144. plot(sx,sy,mode) int sx,sy,mode;
  145. {
  146. int vdcaddr,a,x,y,mflag;
  147. extern int currentx,currenty;
  148. char maskbyte,vdcbyte,addrlo,addrhi,portreg,regbyte;
  149.  
  150. static int mask[8] = { 128,64,32,16,8,4,2,1 };
  151.     vdcaddr = sy*80+sx/8;
  152.     maskbyte = mask[(sx % 8)];
  153.     addrhi = vdcaddr >> 8;
  154.     addrlo = vdcaddr;
  155.     portreg = 18;
  156.     regbyte = addrhi;
  157.     sys(0xf,0xcdcc,®byte,&portreg,&y);
  158.     portreg = 19;
  159.     regbyte = addrlo;
  160.     sys(0xf,0xcdcc,®byte,&portreg,&y);
  161.     sys(0xf,0xcdd8,&vdcbyte,&x,&y);
  162.     portreg = 18;
  163.     regbyte = addrhi;
  164.     sys(0xf,0xcdcc,®byte,&portreg,&y);
  165.     portreg = 19;
  166.     regbyte = addrlo;
  167.     sys(0xf,0xcdcc,®byte,&portreg,&y);
  168.     if (mode = 1){
  169.         regbyte = (vdcbyte  maskbyte);
  170.         goto plotter;
  171.     }
  172.     if (mode = 2) {
  173.         regbyte = (vdcbyte & (maskbyte));
  174.         goto plotter;
  175.     }
  176.     if (mode = 0 ){
  177.         maskbyte = maskbyte;
  178.         regbyte = (vdcbyte & maskbyte);
  179.         goto plotter;
  180.     }
  181.     if (mode>2  mode<0)
  182.         return(0);
  183.  
  184.     plotter:
  185.  
  186.     sys(0xf,0xcdca,®byte,&portreg,&y); 
  187.     return(1);
  188. }
  189.  
  190. draw(x1,y1,x2,y2,mode) int x1,x2,y1,y2,mode;
  191. {
  192. extern currentx,currenty;
  193. int dx,dy,adx,ady,xa,ya,d,inc1,inc2;
  194.  
  195.     plot(x1,y1);
  196.     dx = x2-x1;
  197.     dy = y2-y1;
  198.  
  199.     if(dx<0) {
  200.         xa = -1;
  201.         adx = -dx;
  202.     } else {
  203.         xa = 1;
  204.         adx = dx;
  205.     }
  206.     if(dy<0) {
  207.         ya = -1;
  208.         ady = -dy;
  209.     } else {
  210.         ya = 1;
  211.         ady = dy;
  212.     }
  213.  
  214.     if(ady>ady) {
  215.         inc1 = ady << 1;
  216.         d = inc1 - adx;
  217.         inc2 = inc1 - (adx << 1);
  218.         
  219.         while(x1 != x2) {
  220.             x1 += xa;
  221.             if(d<0)
  222.                 d += inc1;
  223.             else {
  224.                 y1 += ya;
  225.                 d += inc2;
  226.             }
  227.             plot(x1,y1);
  228.         }
  229.     } else {
  230.         inc1 = adx << 1;
  231.         d = inc1 - ady;
  232.         inc2 = inc1 - (ady << 1);
  233.  
  234.         while(y1 != y2) {
  235.             y1 += ya;
  236.             if(d<0)
  237.                 d += inc1;
  238.             else {
  239.                 x1 += xa;
  240.                 d += inc2;
  241.             }
  242.             plot(x1,y1);
  243.         }
  244.     }
  245.     currentx = x2;
  246.     currenty = y2;
  247. }
  248.  
  249. drawto(x,y,mode) int x,y,mode;
  250. {
  251. extern int currentx,currenty;
  252.  
  253.     draw(currentx,currenty,x,y,mode);
  254.     currentx = x;
  255.     currenty =y;
  256. }
  257.  
  258. drawrel(dx,dy,mode) int dx,dy,mode;
  259. {
  260. extern currentx,currenty;
  261.     
  262.     draw(currentx,currenty,currentx+dx,currenty+dy,mode);
  263.     currentx += dx;
  264.     currenty += dy;
  265. }
  266.  
  267. hline(length,mode) int length,mode;
  268. /* draws a horz. line fron the current X Y coords */
  269. {
  270. extern int currentx,currenty;
  271. int i,end;
  272.  
  273.     end =currentx+length;
  274.     if (length >0 )
  275.        for (i=currentx ; i<= end ; i++) plot(i,currenty,mode);
  276.     else 
  277.         for (i=end ; i<= currentx ; i++) plot(i,currenty,mode);
  278.     currentx = end;    
  279. }
  280.  
  281. vline(length,mode) int length,mode;
  282. /* draws a vertical line from the current X Y coords */
  283. {
  284. extern int currentx,currenty;
  285. int i,end;
  286.  
  287.     end = currenty+length;
  288.     if(length>0)
  289.         =or(i=currenty ; i<=end ; i++) plot(currentx,i,mode);
  290.     else
  291.         for(i=end ; i<=currenty ; i++) plot(currentx,i,mode);
  292.     currenty = end;    
  293. }
  294.  
  295. box(x,y,x1,y1,mode) int x,y,x1,y1,mode;
  296. /* draws a box of h & w from current X Y coordinates */
  297. {
  298.     draw(x,y,x1,y,mode);
  299.     draw(x1,y,x1,y1,mode);
  300.     draw(x1,y1,x,y1,mode);
  301. r   draw(x,y1,x,y,mode);
  302. }
  303.  
  304. scale(y) int y;
  305. /* map y coordinate in to 640 X 450 screen */
  306. {
  307.     if (y<=449){ 
  308.         y = 449-y;
  309.         return(y*20/45);
  310.     }
  311.     else
  312.         return(-1);
  313. }
  314.  
  315. /**********************************************1******************************/
  316. /*                 Cursor location routines for hires mode                   */
  317. /*                                                                           */
  318. /*  Current location is in the global variables currentx & currenty, drawto, */
  319. /*  drawrel,hline,vline and box all use these variables. Draw sets current   */
  320. /*  X & Y to it's last coordinates. Plot dosen't change currentx or currenty */
  321. /*  or  reference them.                                                      */
  322. /*                                                                           */
  323. /*****************************************************************************/
  324.  
  325. wherex()
  326. {
  327. extern int currentx;
  328.     return(currentx);
  329. }
  330.  
  331. wherey()
  332. {
  333. extern int currenty;
  334.     return(currenty);
  335. }
  336.  
  337. gotoxy(x,y) int x,y;
  338. {
  339. extern int currentx,currenty;
  340.     currentx = x;
  341.     currenty = y;
  342. }
  343.